// * *----------------------检测用户名---------------------------* *//
var rname=false;
var rpassword=false;
var conRPassword=false;
var remail=false;
var rvf=false;

function chkUserName() {
	var charCode = /^[\u4e00-\u9fa5\da-zA-Z\ \-\_]+$/;
	var name = $("#name").val();
	if (name != '') {
		if (!charCode.test(name)) {
			$("#username_notice").html("<font color='red'>用户名不能含有非法字符!</font>");
			rname=false;
			return false;
		}
		$.ajax({
			type : "get",
			url : "regist/CheckName",
			data : "name=" + encodeURIComponent(name),
			success : function(data) {
				if (data == 1) {
					$("#username_notice").html("<font color='red'>用户名已经被占用!</font>");
					rname=false;
					return false;
				} else if (data == 0) {
					$("#username_notice").html("<font color='blue'>√</font>");
					rname=true;
					return true;
				}
			},
			beforeSend : function() {
				$("#username_notice").html("<img  src='EXTENT_PATH/images/loading.gif'>检测用户名中.....");
			}

		});
	}
}
function checkEmail() {
	var email = $("#email").val();
	var pattern = /^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,4}$/i;
	if (email != '') {
		if (!pattern.test(email)) {
			$("#email_notice").html("<font color='red'>邮箱格式不正确!</font>");
			remail=false;
			return false;
		} 
		$.ajax({
			type : "get",
			url : "regist/CheckEmail",
			data : "email=" + email,
			success : function(data) {
				if (data == 1) {
					$("#email_notice").html("<font color='red'>此邮箱已经注册过，请直接登录!</font>");
					remail=false;
					return false;
				} else if (data == 0) {
					$("#email_notice").html("<font color='blue'>√</font>");
					remail=true;
					return true;
				}
			},
			beforeSend : function() {
				$("#email_notice").html("<img  src='EXTENT_PATH/images/loading.gif'>检测邮箱中.....");
			}
	
		});
	}

}

function check_password(){
	var password=$("#password").val();
		if(password==''){
			$("#password_notice").html("<font color='red'>请填写密码!</font>");
			rpassword=false;
			return false;
		}
		else{
			if(password.length<6){
				$("#password_notice").html("<font color='red'>密码不能小于6位!</font>");
				rpassword=false;
				return false;

			}else{
				$("#password_notice").html("<font color='blue'>√</font>");
				rpassword=true;
				return true;
			}
		}
}
//--------------------密碼难度----------------------------//

function checkIntensity(pwd) {
	var Mcolor = "#FFF", Lcolor = "#FFF", Hcolor = "#FFF";
	var m = 0;

	var Modes = 0;
	for (i = 0; i < pwd.length; i++) {
		var charType = 0;
		var t = pwd.charCodeAt(i);
		if (t >= 48 && t <= 57) {
			charType = 1;
		} else if (t >= 65 && t <= 90) {
			charType = 2;
		} else if (t >= 97 && t <= 122)
			charType = 4;
		else
			charType = 4;
		Modes |= charType;
	}

	for (i = 0; i < 4; i++) {
		if (Modes & 1)
			m++;
		Modes >>>= 1;
	}

	if (pwd.length <= 4) {
		m = 1;
	}

	switch (m) {
	case 1:
		Lcolor = "2px solid red";
		Mcolor = Hcolor = "2px solid #DADADA";
		break;
	case 2:
		Mcolor = "2px solid #f90";
		Lcolor = Hcolor = "2px solid #DADADA";
		break;
	case 3:
		Hcolor = "2px solid #3c0";
		Lcolor = Mcolor = "2px solid #DADADA";
		break;
	case 4:
		Hcolor = "2px solid #3c0";
		Lcolor = Mcolor = "2px solid #DADADA";
		break;
	default:
		Hcolor = Mcolor = Lcolor = "";
		break;
	}
	document.getElementById("pwd_lower").style.borderBottom = Lcolor;
	document.getElementById("pwd_middle").style.borderBottom = Mcolor;
	document.getElementById("pwd_high").style.borderBottom = Hcolor;

}

// --------------------密碼难度结束----------------------------//
function check_conform_password() {
	var conform_password = document.getElementById("conform_password");
	password = document.getElementById('password').value;
	if (conform_password.value != '' && password != '') {
		if (conform_password.value != password) {
			document.getElementById('conform_password_notice').innerHTML = "<span style='COLOR:#ff0000'>两次输入不一致！</span>";
			conRPassword=false;
			return false;
		} else {
			document.getElementById('conform_password_notice').innerHTML = "<span style='COLOR:#333399'>√</span>";
			conRPassword=true;
			return true;

		}
	}

}
function checkVerify() {
	var code = document.getElementById("VerifyCode").value;
	if (code == "") {
		document.getElementById('VerifyCode_notice').innerHTML = "<span style='COLOR:#ff0000'>*请输入验证码</span>";
		rvf=false;
		return false;
	} else {
		$.ajax({
			type : "get",
			url : "CheckVerifycode",
			data : "code=" + code,
			success : function(data) {
				if (data == 0) {
					$("#VerifyCode_notice").html("<font color='blue'>√</font>");
					rvf=true;
					return true;
				} else if (data == 1) {
					$("#VerifyCode_notice").html("<font color='red'>验证码输入错误!</font>");
					rvf=false;
					return false;
				}
			},
			beforeSend : function() {
				$("#VerifyCode_notice").html("<img  src='EXTENT_PATH/images/loading.gif'>验证中.....");
			}

		});
	}
}
function regist() {
	var name = $("#name").val();
	var email = $("#email").val();
	var password = $("#password").val;
	var conform_password = $("#conform_password").val();
	var VerifyCode = $("#VerifyCode").val();

	if(name==''){
		$("#username_notice").html("<font color='red'>请填写用户名</font>");
		$("#name").focus();
		result=false;
		return false;
		
	}else if(email==''){
		$("#email_notice").html("<font color='red'>请填写邮箱!</font>");
		$("#email").focus();
		result=false;
		return false;
	}else if(password==''){
		$("#password_notice").html("<font color='red'>请填写密码!</font>");
		$("#password").focus();
		result=false;
		return false;
	}else if(conform_password==''){
		$("#conform_password_notice").html("<font color='red'>请确认密码!</font>");
		$("#conform_password").focus();
		result=false;
		return false;
	}else if(VerifyCode==''){
		$("#VerifyCode_notice").html("<font color='red'>请填验证码!</font>");
		$("#VerifyCode").focus();
		result=false;
		return false;
	}
	if(!rname){
		$("#name").focus();
		chkUserName();
	}
	if(!remail){
		$("#email").focus();
		checkEmail();
	}
	if(!rpassword){
		$("#password").focus();
		check_password();
	}
	if(!conRPassword){
		$("#conform_password").focus();
		check_conform_password();
	}
	if(!rvf){
		$("#VerifyCode").focus();
		checkVerify();
	}
	
	if(rname && remail && rpassword && conRPassword && rvf){
		$("#registFrom").attr("action", "regist/RegistServlet");
		$("#registFrom").submit();
	}
	

}

window.onload = function() {
	var verifyObj = document.getElementById("Verify");
	verifyObj.onclick = function() {
		this.src = "VerifyCodeServlet";
	};
}